#define _CRT_SECURE_NO_WARNINGS 1


#include "game.h"


void InitBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;

	for (i = 0; i < row; i++)
	{
		int j = 0;

		for (j = 0; j < col; j++)
		{
			board[i][j] = ' ';
		}

	}

}


void DisplayBoard(char board[ROW][COL], int row, int col)
{
	int i = 0;

	for (i = 0; i < row; i++)
	{
		int j = 0;

		for (j = 0; j < col; j++)
		{
			printf(" %c ", board[i][j]);

			if (j < col - 1)
			{
				printf("|");
			}

		}

		printf("\n");

		if (i < row - 1)
		{
			for (j = 0; j < col; j++)
			{
				printf("---");

				if (j < col - 1)
				{
					printf("|");
				}

			}

			printf("\n");
		}

	}

}



void PlayerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("玩家下棋:>\n");
	
	while (1)
	{
		printf("请输入下棋的坐标，中间使用空格:>");
		scanf("%d %d", &x, &y);

		if ((x >= 1) && (x <= row) && (y >= 1) && (y <= col))
		{
			
			if (' ' == board[x - 1][y - 1])
			{
				board[x - 1][y - 1] = '*';
				break;
			}
			else
			{
				printf("坐标被占有，不能落子，重新输入坐标\n");
			}

		}
		else
		{
			printf("坐标非法，重新输入\n");
		}

	}

}



void ComputerMove(char board[ROW][COL], int row, int col)
{
	int x = 0;
	int y = 0;
	printf("电脑下棋:>\n");

	while (1)
	{
		x = rand() % row;
		y = rand() % col;

		if (' ' == board[x][y])
		{
			board[x][y] = '#';
			break;
		}

	}

}



int IsFull(char board[ROW][COL], int row, int col)
{
	int i = 0;

	for (i = 0; i < row; i++)
	{
		int j = 0;

		for (j = 0; j < col; j++)
		{

			if (' ' == board[i][j])
			{
				return 0;
			}

		}

	}

	return 1;
}


char IsWin(char board[ROW][COL], int row, int col)
{
	int i = 0;

	for (i = 0; i < row; i++)
	{
		int j = 0;
		int count = 0;

		for (j = 0; j < col - 1; j++)
		{

			if ((board[i][j]!=' ') && (board[i][j]==board[i][j+1]))
			{
				count++;
			}

		}

		if ((col-1) == count)
		{
			return board[i][0];
		}

	}

	for (i = 0; i < col; i++)
	{
		int j = 0;
		int count = 0;

		for (j = 0; j < row - 1; j++)
		{

			if ((board[j][i]!=' ') && (board[j][i]==board[j+1][i]))
			{
				count++;
			}

		}

		if ((row - 1) == count)
		{
			return board[0][i];
		}

	}

	int count = 0;

	for (i = 0; i < row - 1; i++)
	{
		
		if ((board[i][i]!=' ') && (board[i][i]==board[i+1][i+1]))
		{
			count++;
		}

	}

	if ((row - 1) == count)
	{
		return board[0][0];
	}

	count = 0;

	for (i = 0; i < row - 1; i++)
	{

		if ((board[i][col-1-i]!=' ') && (board[i][col-1-i]==board[i+1][col-2-i]))
		{
			count++;
		}

	}

	if ((row-1) == count)
	{
		return board[0][col - 1];
	}

	if (1 == IsFull(board, row, col))
	{
		return 'Q';
	}

	return 'C';
}